package ACWing.SearchAndGraphTheory.最小生成树;
//859. Kruskal算法求最小生成树

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2023/1/1 20:31
 */
//类似并查集合并操作去添加集合
public class Kruskal{

    static int N=100010;
    static int[]p=new int[N];
    static class Pall{
        int x;
        int y;
        int l;

        public Pall(int x, int y, int l) {
            this.x = x;
            this.y = y;
            this.l = l;
        }
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        Pall[]palls=new Pall[m];
        for (int i = 0; i < m; i++) {
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=sc.nextInt();
            palls[i]=new Pall(a,b,c);
        }
        Arrays.sort(palls,cmp);
        for (int i = 1; i <= n; i++) {
            p[i]=i;
        }
        int sum=0,num=0;
        for (int i = 0; i < m; i++) {
            int a=palls[i].x;
            int b=palls[i].y;
            a=find(a);
            b=find(b);
            if(a!=b){
                p[a]=b;
                sum=sum+palls[i].l;
                num++;
            }
        }
        if(num<n-1){
            System.out.println("impossible");
        }else {
            System.out.println(sum);
        }
    }
    public static int find(int x){
        if(p[x]!=x){
            p[x]=find(p[x]);
        }
        return p[x];
    }
    static Comparator<Pall> cmp=new Comparator<Pall>()
    {
        @Override
        public int compare(Pall o1, Pall o2) {
            return o1.l-o2.l;
        }
        //将两边的权值按照从小到大排序

    };
}
